<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
	  <el-form-item label="缴费年份" prop="inyear">
	    <el-select v-model="queryParams.inyear" placeholder="请选择缴费年份" clearable class="!w-240px">
	      <el-option v-for="year in years" :key="year" :label="year" :value="year" />
	    </el-select>
	  </el-form-item>
      <el-form-item label="会员姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          class="!w-240px"
          clearable
          placeholder="请输入会员姓名"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          class="!w-240px"
          clearable
          placeholder="请输入手机号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缴费时间" prop="ctime">
        <el-date-picker
          v-model="queryParams.ctime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="会员职级" prop="tagIds">
        <MemberTagSelect v-model="queryParams.tagIds" />
      </el-form-item>
      <el-form-item label="会员组别" prop="groupId">
        <MemberGroupSelect v-model="queryParams.groupId" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
		<el-button type="primary" @click="openForm('create')">
		  <Icon icon="ep:plus" class="mr-5px" /> 新增
		</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      :stripe="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" label="会员编号" prop="memId" />
      <el-table-column align="center" label="手机号" prop="mobile" />
      <el-table-column align="center" label="会员姓名" prop="nickname" />
      <el-table-column align="center" label="会员组别" prop="groupName" />
	  <el-table-column align="center" label="会员职级" prop="tagName" />
	  <el-table-column align="center" label="归属年份" prop="inyear" />
	  <el-table-column align="center" label="会费金额" prop="amount" />
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="缴费时间"
        prop="ctime"
        width="180px"
      />
	  <el-table-column align="center" label="备注" prop="remark" />
      <el-table-column
        :show-overflow-tooltip="false"
        align="center"
        fixed="right"
        label="操作"
        width="200px"
      >
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button link type="primary" @click="handleCommand('handleUpdate',scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as ChamberApi from '@/api/chamber'
import UserForm from './UserForm.vue'
import MemberTagSelect from '@/views/member/tag/components/MemberTagSelect.vue'
import MemberGroupSelect from '@/views/member/group/components/MemberGroupSelect.vue'
import { checkPermi } from '@/utils/permission'

defineOptions({ name: 'MemberFee' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  nickname: null,
  mobile: null,
  inyear: new Date().getFullYear(),
  ctime: [],
  tagIds: [],
  groupId: null
})
const queryFormRef = ref() // 搜索的表单
const selectedIds = ref<number[]>([]) // 表格的选中 ID 数组
const years = computed(()=> {
	let x = [];
	for(let i=(new Date().getFullYear())+1; i>=2015 ; i--) {
		x.push(i);
	}
	return x;
});
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ChamberApi.getMemberFeePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 表格选中事件 */
const handleSelectionChange = (rows: ChamberApi.MemberFeeVO[]) => {
  selectedIds.value = rows.map((row) => row.id)
}

/** 操作分发 */
const handleCommand = (command: string, row: ChamberApi.MemberFeeVO) => {
  switch (command) {
    case 'handleUpdate':
      openForm('update', row.id)
      break
    default:
      break
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ChamberApi.deleteMemberFee(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
